#include "StaticMesh.h"
#include "Logger\Logger.h"
#include "Vertex\VertexType.h"
#include "Base.h"
#include "Vertex\IndexedVertexs.h"
#include "RenderManager.h"
#include "Effects/EffectManager.h"
#include "Effects/EffectTechnique.h"
#include "Textures/TextureManager.h"
#include "Textures/Texture.h"

#if defined (_DEBUG)
#include "Memory\MemLeaks.h"
#endif

//-------------------------------------------------
CStaticMesh::CStaticMesh()
	: m_sFilename("")
	, l_pRenderable(NULL)
{

}

//-------------------------------------------------
CStaticMesh::~CStaticMesh()
{
	CleanUp();
}

//-------------------------------------------------
void CStaticMesh::CleanUp()
{
	CHECKED_DELETE(l_pRenderable);
}

//-------------------------------------------------
bool CStaticMesh::Load(const std::string& _filename)
{
	m_sFilename = _filename;

#if defined (_DEBUG)
	LOGGER->AddNewLog(ELL_INFORMATION, "CStaticMesh::Load->Loading the static mesh: %s", m_sFilename.c_str());

	bool l_bIsOk = LoadFile();
	if(l_bIsOk)
		LOGGER->AddNewLog(ELL_INFORMATION, "CStaticMesh::Load->Static mesh loaded correctly.");

	return l_bIsOk;
#else
	return LoadFile();
#endif
}

//-------------------------------------------------
bool CStaticMesh::Reload()
{
	CleanUp();

#if defined (_DEBUG)
	LOGGER->AddNewLog(ELL_INFORMATION, "CStaticMesh::Reload->Reloading the static mesh: %s", m_sFilename.c_str());

	bool l_bIsOk = LoadFile();
	if(l_bIsOk)
		LOGGER->AddNewLog(ELL_INFORMATION, "CStaticMesh::Reload->Static mesh reloaded correctly.");

	return l_bIsOk;
#else
	return LoadFile();
#endif
}

//-------------------------------------------------
bool CStaticMesh::LoadFile()
{
	FILE* l_pFile = NULL;

	fopen_s(&l_pFile, m_sFilename.c_str(), "rb");
	if(l_pFile == NULL)
		return false;

	uint16 tmp = 0;
	uint16 numIndex = 0;
	uint16 numVertexs = 0;
	char* c_name = NULL;
	std::string name = "";
	uint16* l_IdxBuffer = NULL;
	void*	l_VtxBuffer = NULL;

	//Read Name of Object
	fread(&tmp, sizeof(uint16), 1, l_pFile);
	c_name = new char[tmp];
	memset(c_name, 0, tmp);
	fread(c_name, sizeof(char), tmp, l_pFile);
	name = std::string(c_name);
	CHECKED_DELETE(c_name);

	//Read number of vertices
	fread(&numVertexs, sizeof(uint16), 1, l_pFile);
	l_VtxBuffer = new TTEXTURED_VERTEX[numVertexs];
	memset(l_VtxBuffer, 0, numVertexs);
	fread(l_VtxBuffer, sizeof(TTEXTURED_VERTEX), numVertexs, l_pFile);

	//Read number of indexes
	fread(&numIndex, sizeof(uint16), 1, l_pFile);

	//Create Index Buffer
	l_IdxBuffer = new uint16[numIndex];
	memset(l_IdxBuffer, 0, numIndex);

	//Read Index Buffer
	fread(l_IdxBuffer, sizeof(uint16), numIndex, l_pFile);

	l_pRenderable = new CIndexedVertexs<TTEXTURED_VERTEX>(RENDER_MANAGER, l_VtxBuffer, l_IdxBuffer, numVertexs, numIndex); 
	
	fclose(l_pFile);

	return true;
}

//-------------------------------------------------
void CStaticMesh::Render(CRenderManager* _pRM)
{
	// TODO: Obtenir la variable en CEffectTechnique, setejar-li el punter de la textura i pssar-la al shader
	CTexture* l_pTexture = TEXTURE_MANAGER->GetTexture("./data/textures/seafloor.dds");
	assert(l_pTexture);

	RENDER_MANAGER->GetEffectManager()->PushTexture(l_pTexture);

	CEffectTechnique* l_pTechnique = _pRM->GetEffectManager()->GetTechnique("generic");
	l_pRenderable->Render(_pRM, l_pTechnique);
}